<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="医生姓名">
              <el-input v-model="form.doctor_name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="机构名称">
              <el-select v-model="form.hospital_id" placeholder="请选择" clearable>
                <el-option
                  v-for="item in hospitalNameList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建日期">
              <el-date-picker
                v-model="dates"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上下架状态">
              <el-select v-model="form.is_on_shelf" placeholder="请选择" clearable>
                <el-option
                  v-for="item in isOnStatus"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="handleSearch">查询</el-button>
              <el-button style="margin-left:60px" type="primary" @click="toRoutes('AddDoctor')">新增</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column prop="doctor_name" label="医生姓名" />
        <el-table-column prop="hospital_name" label="机构名称" />
        <el-table-column prop="hospitalName" label="机构所属城市" />
        <el-table-column prop="position_cn" label="职位" />
        <el-table-column label="学历职称" prop="education_cn">
          <!-- <template slot-scope="scope">
            <div v-for="i in scope.row.tags">
              <span>{{i}}</span>
              <br />
            </div>
          </template>-->
        </el-table-column>
        <el-table-column label="医生照片">
          <template slot-scope="scope">
            <div>
              <img style="height:40px;width:40px" :src="scope.row.portrait | imgUrlFn" alt />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="美分享上下架">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_on_shelf"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.id,'status',scope.row.status)"
            />
          </template>
        </el-table-column>

        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="toRoutes('doctorAdd',scope.row.id,'edit')"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              @click="toRoutes('doctorAdd',scope.row.id,'detail')"
            >详情</el-button>
            <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { hospitalList, hospitalDel, getHospitalName } from "@/api/couponList";
// import {
//   doctorList,
//   changeDoctorStatus,
//   deleteDoctor,
// } from "@/api/doctorManage";
// import { companyList } from "@/api/branchOfficeDropDown";
import { policy } from "@/api/policy";

export default {
  name: "DoctorIndex",
  filters: {
    imgUrlFn: function (e) {
      return sessionStorage.getItem("imgUrlPrefix") + e;
    },
  },
  data() {
    return {
      form: {
        doctor_name: "",
        hospital_id: "",
        start_date: "",
        end_date: "",
        is_on_shelf: "",
        page: 1,
        pageSize: 10,
      },
      dates: [],
      tableLoading: true,
      tableData: [],
      page_totals: 0,
      page_sizes: [10, 30, 50, 100],
      hospitalNameList: {},
      isOnStatus: [
        { name: "上架", value: 0 },
        { name: "下架", value: 1 },
      ],
    };
    // return {
    //   imgUrl: '',
    //   tableLoading: true,
    //   officeOption: [],
    //   statusOption: [
    //     {
    //       value: '0',
    //       label: '正常'
    //     },
    //     {
    //       value: '1',
    //       label: '停用'
    //     }
    //   ],
    //   isRecommendOption: [
    //     {
    //       value: '0',
    //       label: '不推荐'
    //     },
    //     {
    //       value: '1',
    //       label: '推荐'
    //     }
    //   ],
    //   form: {
    //     name: '',
    //     hospitalId: '',
    //     status: '',
    //     isRecommend: '',
    //     pageNum: 1,
    //     pageSize: 10
    //   },
    //   tableData: [],
    //   page_sizes: [10, 30, 50, 100],
    //   page_totals: 60,
    //   listForm: {
    //     pageNum: 1,
    //     pageSize: 10
    //   }
    // }
  },
  mounted() {
    this.getList(this.form);

    this.handleHosName(this.form.hospital_id);
    // 分公司列表
    // this.getCompanyList();
    // this.getList(this.listForm);
    // 获取图片路径
    this.getPolicy();
  },
  methods: {
    // 路由跳转
    toRoutes(e, id, type) {
      // console.log(e, id, type);
      const route = { path: e, query: { id: id, type: type } };
      this.$router.push(route);
    },
    // 编号
    table_index(index) {
      return (this.form.page - 1) * this.form.pageSize + index + 1;
    },
    // 获取列表
    getList(opt) {
      hospitalList(opt).then((res) => {
        this.tableLoading = false;
        if (res.data.code == 0) {
          this.tableData = res.data.data.list;
          this.page_totals = res.data.data.total;
          this.hospitalNameList = res.data.data.hospitalArray;
        } else {
          this.tableLoading = false;
          this.warnStatus(res.data.msg, "warning");
        }
      });
    },
    // 获取机构名称
    handleHosName() {
      let data = {
        keyword: "",
      };
      getHospitalName(data).then((res) => {
        console.log("res2", res);
      });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 删除
    handleDel(id) {
      let data = { id };
      this.$confirm("此操作将会删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          hospitalDel(data)
            .then((res) => {
              if (res.data.code == 0) {
                this.warnStatus("删除成功", "success");
                this.getList();
              } else {
                this.warnStatus("删除成功", "warning");
              }
            })
            .catch((err) => {
              this.errorStatus(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
    // 成功状态弹窗
    warnStatus(txt, status) {
      this.eleMsg(txt, status);
      // this.eleMsg(txt, "warning");
    },
    // 获取签名
    getPolicy() {
      policy().then((res) => {
        // console.log(res)
        const aliyunOssToken = res.data.data;
        const imgUrlPrefix =
          aliyunOssToken.host + "/" + aliyunOssToken.dir + "/";
        this.imgUrl = imgUrlPrefix;
        sessionStorage.setItem("imgUrlPrefix", imgUrlPrefix);
      });
    },
    // 上下架与状态修改
    changeStatus(id, type, status) {
      const datas = {
        id: id + "",
      };
      datas[type] = status + "";
      changeDoctorStatus(datas).then((res) => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
        } else {
          this.$message.error("修改失败");
        }
      });
      // console.log(datas)
    },
    // 获取分公司列表
    getCompanyList() {
      companyList()
        .then((res) => {
          const resData = res.data.data;
          const newData = [];
          resData.forEach((e) => {
            newData.push({
              value: e.id,
              label: e.name,
            });
          });
          this.officeOption = newData;
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },

    // 查询
    searchFn() {
      this.form.hospitalId = this.form.hospitalId + "";
      // console.log(this.form)
      this.getList(this.form);
    },

    // 列表数据获取
    // 删除
    deleteFn(e) {
      const datas = {
        id: e,
      };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteDoctor(datas)
            .then((res) => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("删除");
                this.getList(this.form);
              } else {
                this.$message.error("数据接口访问失败");
              }
            })
            .catch((error) => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
